<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
              <cite>用户展示</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
<!--                <div class="layui-card-body ">-->
<!--                    <form class="layui-form layui-col-space5">-->


<!--                    </form>-->
<!--                </div>-->
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加用户','/user/insert',600,400)"><i class="layui-icon"></i>添加</button>
                    <input type="file" id="imgBox">
                    <button onclick="uploadImg()">上传图片</button>

                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>
                                <input type="checkbox" name=""  lay-skin="primary">
                            </th>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>年龄</th>
                            <th>日期</th>
                            <th>操作</th>
                        </thead>
<!--                        <tr th:each="item:${list}">-->
<!--                            <td>-->
<!--                                <input type="checkbox" name=""  lay-skin="primary">-->
<!--                            </td>-->
<!--                            <td th:text="${item.id}">-->
<!--                            <td th:text="${item.name}">-->
<!--                            <td th:text="${item.age}">-->
<!--                            <td th:text="${#dates.format(item.day,'yyyy-MM-dd')}">-->


<!--                            <td class="td-manage">-->
<!--                                <a title="编辑"  onclick="xadmin.open('编辑','/user/update')" href="javascript:;">-->
<!--                                    <i class="layui-icon">&#xe642;</i>-->
<!--                                </a>-->
<!--                                <a title="删除" onclick="del('/' + ${item.id} + '/')" href="javascript:;">-->
<!--                                    <i class="layui-icon">&#xe640;</i>-->
<!--                                </a>-->
<!--                            </td>-->
<!--                        </tr>-->

                        <tbody id="tbody">

                        </tbody>
                    </table>
                    <script>
                        function uploadImg(){
                            var file=document.getElementById("imgBox").files[0];
                            var formData=new FormData();
                            formData.append("file",file);
                            $.ajax({
                                url:"/user/upload",
                                type:"post",
                                data:formData,
                                processData:false,
                                contentType:false,
                                success(res){
                                    console.log(res)
                                }
                            })
                        }



                        function initData() {
                            $.ajax({
                                url: "/user/index",
                                type: "post",

                                success(res) {
                                    console.log(res)
                                    if (res.success) {
                                        for (var item of res.result)
                                            $("#tbody").append(`
                    <tr>
                    <td><input type="checkbox" name=""  lay-skin="primary"></td>
                    <td>${item.id}</td>
                     <td>${item.name}</td>
                     <td>${item.age}</td>
                      <td>${item.day}</td>
                      <td>
                      <a title="编辑"  onclick="xadmin.open('编辑','/user/update?id=${item.id}')" href="javascript:;">
                                    <i class="layui-icon">&#xe642;</i>
                                </a>
                                <a title="删除" onclick="del(${item.id})" href="javascript:;">
                                    <i class="layui-icon">&#xe640;</i>
                      </td>

                     </tr>
                    `)
                                    } else {
                                        alert(res.mes)
                                    }
                                }
                            })
                        }


                        initData()

                        function del(id){
                            $.ajax({
                                url : "/user/delete",
                                type : "post",
                                data: {
                                    id
                                },
                                success(res){
                                    if (res.success) {
                                        //删除成功跳回
                                        alert(res.msg)
                                        $('#tbody').html('')
                                        initData()
                                    } else {
                                        alert(res.msg);
                                    }
                                }
                            })
                        }



                    </script>



                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        <div>
                            <a class="prev" href="">&lt;&lt;</a>
                            <a class="num" href="">1</a>
                            <span class="current">2</span>
                            <a class="num" href="">3</a>
                            <a class="num" href="">489</a>
                            <a class="next" href="">&gt;&gt;</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

</html>